package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.QueryObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Administrator on 2020/7/12 0012.
 */
@Controller
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * @RequestMapping : 用来接收前台浏览器发送过来的请求
     * 如果匹配上那么会进到这个注解所对应的方法上,
     *
     * @return 在springMVC当中如果返回的是一个字符串, 那么这个字符串是你要返回视图的路径
     */

    @RequestMapping("/role/list")
    public String list(Model model, QueryObject queryObject){
        model.addAttribute("result",roleService.query(queryObject));
        return "role/list";
    }

    @RequestMapping("/role/delete")
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list.do";
    }

    @RequestMapping("/role/input")
    public String input(Model model,Long id){
        //如果id不等null ,那么说明是编辑操作,
        // 编辑操作要求数据回显.
        if(id != null){
            Role role = roleService.get(id);
            model.addAttribute("entity",role);
        }
        return "role/input";
    }


    @RequestMapping("/role/saveOrUpdate")
    public String saveOrUpdate(Role role){
        roleService.saveOrUpdate(role);
        return "redirect:/role/list.do";
    }



}
